package com.itany.corejava.day11;

import javax.swing.*;
import java.awt.*;

public class Calculator {

    //定义组件
    private JFrame frame;
    private JTextField field;
    private JButton clearButton;
    private JButton[] allButtons;

    //创建组件
    public Calculator() {
        frame=new JFrame("计算器");
        field=new JTextField("0",19);
        clearButton=new JButton("C");

        String s="123+456-789*0.=/";
        allButtons=new JButton[s.length()];
        for (int i = 0; i < allButtons.length; i++) {
            allButtons[i]=new JButton(s.charAt(i)+"");
        }

    }

    //布局
    private void init(){
        frame.setLayout(new BorderLayout(3,3));

        JPanel northPanel=new JPanel();
        JPanel centerPanel=new JPanel();

        northPanel.setLayout(new FlowLayout());
        centerPanel.setLayout(new GridLayout(4,4,6,6));

        northPanel.add(field);
        northPanel.add(clearButton);

        for (int i = 0; i < allButtons.length; i++) {
            centerPanel.add(allButtons[i]);
        }

        frame.add(northPanel,BorderLayout.NORTH);
        frame.add(centerPanel,BorderLayout.CENTER);


    }

    //设置样式
    private void setStyle(){
        field.setHorizontalAlignment(JTextField.RIGHT);

        field.setEditable(false);

        field.setBackground(Color.WHITE);
        field.setForeground(new Color(0, 0, 0));


        clearButton.setFont(new Font("Courier New",Font.BOLD+Font.ITALIC,16));


        Font font=new Font("Courier New",Font.PLAIN,16);

        for (int i = 0; i < allButtons.length; i++) {
            allButtons[i].setFont(font);
        }



    }


    //显示窗口
    public void show(){
        init();
        setStyle();

        frame.setSize(285,200);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    //运行
    public static void main(String[] args) {
        new Calculator().show();;
    }























}
